// miniprogram/pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    dishi: ['请选择地市'],
    mingcheng: ['请选择您所在的考点名称'],
  },
  tijiao: function (e) {
    var that = this;
    var phone = wx.getStorageSync('phone');
    var dishi = that.data.dishi[that.data.index];
    var mingcheng = that.data.mingcheng[that.data.index2];

    if(phone){
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/writelogs/?actid=34248&callback=?",
        data: {
          phone:phone,
          dishi: dishi,
          mingcheng: mingcheng
        },
      })
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=34248&callback=?",
        data: {
          dishi: dishi,
          mingcheng: mingcheng
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson)
          if(theJson.status == 1){
            wx.setStorageSync('lists', theJson.lists)
            wx.navigateTo({
              url: '../success/success',
            })
          } else {
            wx.showToast({
              title: '暂无数据',
              duration:2000,
              icon:'none'
            })
          }
         
        }
      })
    }  else {
      wx.showToast({
        title: '请先登录',
        duration:2000,
        icon:'none'
      })
      that.setData({
        fixed:1
      })
    }
  },

  getPhoneNumber(e) {
    var that = this;
    var fenxiao = that.data.fenxiao;
    var geneal = that.data.geneal;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      wx.setStorageSync('phone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=34248&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: fenxiao,
          geneal: geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          that.setData({
            fixed:!1
          })
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      fenxiao: options.fenxiao,
      geneal: options.geneal
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34248&callback=?', //接收url
      data: {
        level: '1',
        dishi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择地市')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].dishi)
        }
        that.setData({
          dishi: list
        });
      }
    })
  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('dishi发送选择改变，携带值为', that.data.dishi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34248&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'dishi',
        grtext: that.data.dishi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].mingcheng)
        }
        that.setData({
          mingcheng: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('mingcheng发送选择改变，携带值为', that.data.mingcheng[num2]);
    this.setData({
      index2: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})